<?php
namespace app\admin\controller;

use think\Db;
use think\Response;
use think\Session;
use think\View;
use think\Request;

class Admin extends Common
{
    public function admin(){
        if (Request::instance()->isAjax()){
            $where['is_del'] = array('eq',0);
            $data = Db::name('admin')->alias('t1')->field('t1.*,t3.title as role_name,t3.id as role_id
')->join('lv_auth_group_access t2','t1.admin_id = t2.uid','left')
                ->join('lv_auth_group t3','t2.group_id = t3.id','left')
                    ->where($where)->order('t1.admin_id','desc')->select();
            return ['code'=>0,'msg'=>'','data'=>$data];
        }else{
            $role = Db::name('auth_group')->select();
            $this->assign('role',$role);
            return view();
        }
    }

    /**
     * 管理员添加
     */
    public function addAdmin(){
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $post = $post['data'];
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $post['admin_password'] = $hasher->HashPassword($post['admin_password']);
            //检查用户名是否存在
            $check = Db::name('admin')->where('admin_name',$post['admin_name'])->find();
            if ($check){
                return ['code'=>'用户名已存在'];
                exit;
            }
            $role_id = $post['role_id'];
            if(empty($role_id)){
                return ['code'=>'请先去添加角色'];
                exit;
            }
            unset($post['role_id']);
            $check_phone = Db::name('admin')->where('admin_phone',$post['admin_phone'])->find();
            if($check_phone){
                return ['code'=>'手机号已经存在'];
            }
            $post['addtime'] = date('Y-m-d H:i:s');
            $res = Db::name('admin')->insertGetId($post);
            if ($res){
                $group = array('uid'=>$res,'group_id'=>$role_id);
                Db::name('auth_group_access')->insert($group);
                return ['code'=>0];

            }else{
                return ['code'=>'添加失败'];
            }
        }
    }

    /**
     * 管理员修改
     */
    public function editAdmin(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $saveData = array(
                'admin_name'=>$post['data']['admin_name'],
                'admin_relname'=>$post['data']['admin_relname'],
                'admin_nickname'=>$post['data']['admin_nickname']
            );

            $where['admin_name'] = array('eq',$post['data']['admin_name']);
            $where['admin_id'] = array('neq',$post['data']['admin_id']);

            //检查用户名是否存在
            $check = Db::name('admin')->where($where)->find();
            if ($check){
                return ['code'=>'用户名已存在'];
                exit;
            }

            $res = Db::name('admin')->where('admin_id',$post['data']['admin_id'])->update($saveData);
            $role = Db::name('auth_group_access')->where("uid",$post['data']['admin_id'])->setField('group_id',$post['data']['role_id']);
            if ($res!==false && $role!==false){

                return ['code'=>0];
            }else{
                return ['code'=>'修改失败'];
            }
        }
    }

    /**
     * 管理员密码修改
     */
    public function editAdminPass(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $post = $post['data'];
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //将密码进行哈希，结果是一个长度为60个字符的字符串
            $post['admin_password'] = $hasher->HashPassword($post['admin_password']);

            $res = Db::name('admin')->where('admin_id',$post['admin_id'])->setField('admin_password',$post['admin_password']);
            if ($res!==false){
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }

    /**
     * 管理员删除
     */
    public function delAdmin(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();

            if ($post['admin_id'] == 1){
                return ['code'=>2];
                exit;
            }

            $res = Db::name('admin')->where('admin_id',$post['admin_id'])->setField('is_del',1);
            if ($res!==false){
                return ['code'=>0];
            }else{
                return ['code'=>1];
            }
        }
    }

    /**
     * 管理员禁用
     */
    public function editAdminIsUse(){
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();

            $res = Db::name('admin')->where('admin_id',$post['admin_id'])->setField('is_use',$post['is_use']);
            if ($res!==false){
                return ['code'=>0,'nickname'=>$res['nickname'],'phone'=>$res['phone']];
            }else{
                return ['code'=>1];
            }
        }
    }

    /*
     * 前台身份确认
     */
    public function info()
    {
        if(Request::instance()->isAjax()){
            $post = Request::instance()->post();

            $res = Db::name('user')->where('phone',$post['phone'])->find();
            if($res){

                return ['code'=>0,'nickname'=>$res['nickname'],'phone'=>$res['phone']];
            }else{
                return ['code'=>1];
            }
        }
    }



}